﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZP.UI.PopWindows;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZP.UI
{
    public partial class ZP17 : EFViewCmes
    {
        public ZP17()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 点击所选择的列表发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            LoadView();
        }

        /// <summary>
        /// 加载ZP18角色列表页面
        /// </summary>
        private void LoadView()
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("userId", _row["loginName"].ToString());
                this.area1.LoadView<EFCustomViewContainerPanel>("ZP18", args);//加载从表页面
            }
            else
            {
                this.area1.Content = null;
            }
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnAddButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.Grid.CheckedEIRows.Count == 1)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set(EIConstants.ServiceNameKey, "ZP17");
                eiInfo.Set(EIConstants.MethodNameKey, "queryRoles");
                eiInfo.Set("userId", this.Grid.CheckedEIRows[0]["loginName"]);
                eiInfo.Set("methodLabel", "add");
                eiInfo.Set("title", "添加角色");
                this.Submit(eiInfo, QueryResCompleted);
            }
            else
            {
                this.OpenEFWindowAlert("请选择一条记录进行操作！", true);
                return;
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.Grid.CheckedEIRows.Count == 1)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set(EIConstants.ServiceNameKey, "ZP17");
                eiInfo.Set(EIConstants.MethodNameKey, "queryDelRoles");
                eiInfo.Set("userId", this.Grid.CheckedEIRows[0]["loginName"]);
                eiInfo.Set("methodLabel", "delete");
                eiInfo.Set("title", "删除角色");
                this.Submit(eiInfo, QueryResCompleted);
            }
            else
            {
                this.OpenEFWindowAlert("请选择一条记录进行操作！", true);
                return;
            }
        }

        /// <summary>
        /// 查询资源组的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryResCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                AddRoles view = new AddRoles();
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(e.EIInfo);

                view.userId.Value = this.Grid.CheckedEIRows[0]["loginName"];
                view.Method = e.EIInfo.Get("methodLabel").ToString();

                if ("delete".Equals(e.EIInfo.Get("methodLabel").ToString()))
                {
                    view.queryArea.Visibility = Visibility.Collapsed;
                    view.tiltle = "删除";
                }

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = e.EIInfo.Get("title").ToString();//设置Title
                _popup.Content = view;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            }
            else
            {
                BaseTools.OpenEFWindowAlert("查询角色失败:" + e.EIInfo.Msg, null);
                TempButton.IsEnabled = true;
            }

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                BaseTools.OpenEFWindowAlert("操作成功！", null);
                if (this.Grid.CheckedEIRows[0].Equals((this.Grid.SelectedItem as UIBaseEntity).GetEIRow()))
                {
                    LoadView();
                }
            }

            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
